package com.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Userinfo;

public class checklog implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub

		//将ServletRequest强制转换成他的子类HttpServletRequest便于获得session作用域
		HttpServletRequest req=(HttpServletRequest)request;
		//获得session作用域，并获取放在里面的用户对象
		Userinfo us=(Userinfo)req.getSession().getAttribute("loger");
		//进行判断，若不为空，就提交请求，若为空跳到登陆界面
		if(us!=null){
			//在进行下一步前执行过滤，检测是否登录
			chain.doFilter(request, response);
		} else{
				//
			HttpServletResponse res=(HttpServletResponse)response;
			//由于过滤器是过滤的文件和文件在一个路径下，（和过滤的文件是一个路径）
			res.sendRedirect("../login.jsp");
		}

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
